<template>
	<view class="custom-upload-box">
		<view class="custom-img upload-box-item" v-for="(item,index) in fileList" @click="previewImage(item.path)">
			<image style="width: 100%;height: 100%;" :src="item.path"></image>
			<view class="custom-img-close" @click.stop="closeImage(item.path)">
				<i-close theme="multi-color" size="10" :fill="['#fff']"/>
			</view>
			<view class="custom-img-progress" v-if="item.status != 1">
				<view class="progress-circle" v-if="item.status == 0" :style="{ 'background-image': `conic-gradient(rgba(0,0,0,0.3) ${ (100 - item.time) * 3.6 }deg,#fff ${ (100 - item.time) * 3.6 }deg)` }">{{ 100 - item.time }}%</view>
				<i-error-picture theme="multi-color" v-if="item.status == 2" size="30" :fill="['#ccc']"/>
			</view>
		</view>
		<view class="custom-upload upload-box-item" @click="chooseFile">
			<i-plus theme="multi-color" size="25" :fill="['#333' ,'#2F88FF' ,'#FFF' ,'#43CCF8']"/>
		</view>
	</view>
	<!-- <view>{{ count }}</view> -->
</template>

<script setup>
import { nextTick, reactive, ref } from 'vue'
const file = ref(null);
const fileList = ref([]);

const chooseFile = () => {
	uni.chooseImage({
		success: (res) => {
			console.log(res);
			const tempFilePaths = res.tempFilePaths;
			let arr = {
				name: tempFilePaths[0].split('/').pop(),
				path: tempFilePaths[0],
				//0: 上传中 1: 上传成功 2: 上传失败
				status: 0,
				time: 100,
				timer: null
			};
			fileList.value.push(arr);
			arr.timer = setInterval(()=>{		
				fileList.value[0].time = fileList.value[0].time - 1;
				if(fileList.value[0].time == 0){
					arr.status = 1;
					// arr.status = 2;
					clearInterval(arr.timer);	
				}
			},100);
			
		}
	})
}

const previewImage = (img) => {
	uni.previewImage({
		urls: [img],
		longPressActions: {
			success: function(data) {
				console.log(data);
			},
			fail: function(err) {
				console.log(err);
			}
		}
	});
}

const closeImage = (img) => {
	console.log(img);
	fileList.value = fileList.value.filter(item=>item.path != img);
}

const uploadFile = () => {
	if(!file.value) {
		uni.showToast({
			title: '请先选择文件',
			icon: 'none'
		})
		return;
	}
}
</script>

<style lang="scss">
.custom-upload-box {
	display: flex;
	gap: 20rpx;
	flex-wrap: wrap;
	.upload-box-item {
		width: 200rpx;
		height: 200rpx;
		border-radius: 10rpx;
		box-sizing: border-box;
	}
	.custom-img {
		position: relative;
		overflow: hidden;
		.custom-img-close {
			position: absolute;
			background-color: #000;
			border-radius: 50%;
			width: 70rpx;
			height: 70rpx;
			top: 0;
			right: 0;
			display: flex;
			align-items: flex-end;
			transform: translate(50%,-50%);
			box-sizing: border-box;
			padding-left: 10rpx;
			padding-bottom: 20rpx;
		}
		.custom-img-progress {
			position: absolute;
			width: 100%;
			height: 100%;
			top: 0;
			left: 0;
			background-color: rgba(0,0,0,0.3);
			display: flex;
			align-items: center;
			justify-content: center;
			.progress-circle {
				width: 50%;
				height: 50%;
				border-radius: 50%;
				background-image: conic-gradient(red 35deg,yellow 35deg);
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 20rpx;
			}
		}
	}
	.custom-upload {
		border: 4rpx dashed black;
		display: flex;
		align-items: center;
		justify-content: center;
	}
}
</style>